<?php
$codigo_html = "";
$codigo_html .= "\n<div id='map' style='float:left; width: 600px; height: 500px'>Aguarde un instante mientras se carga el mapa.</div>";
$codigo_html .= "\n<div id='referencias' style='padding-left:10px; float:left; width: 100px; height: 500px'>";
$codigo_html .= "\n<b>Referencias</b>";
$codigo_html .= "\n<br/>";
$codigo_html .= "\n" . $presentacion->image("marcadores/verde.png", array("width"=>"10px", "height"=>"10px"));
$codigo_html .= "\nHumedad < 8";
$codigo_html .= "\n<br/>";
$codigo_html .= "\n" . $presentacion->image("marcadores/rojo.png", array("width"=>"10px", "height"=>"10px"));
$codigo_html .= "\nHumedad > 8";
$codigo_html .= "\n</div>";












//local
$codigo_html .= "\n<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAA2XIlarYB9hkOnQR02Sa-vxQj2krWR2WCSonZIvycAibClc-LYhSB3UNWs71u65y9z7VkHgEAsimSUw' type='text/javascript'></script>";
//pgtdemo
//$codigo_html .= "\n<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAac60cfu93AuYNtIBqo2aixSP7NynNsxMnfZbqdl_SFR-tZZvwxRmdM0d-6qKjo7UArlSo01brkw3JQ' type='text/javascript'></script>";

$codigo_html .= "\n" . $javascript->codeblock('

	//crea un marcador	
	function crearMarcador(punto, icono, html) {
		var marcador = new GMarker(punto, icono);
		GEvent.addListener(marcador, "click", function() {
			marcador.openInfoWindowHtml(html);
		});
		return marcador;
	}

	function CustomGetTileUrl (a,b){
		return "";
	    //return "http://www.pragmatia.com/" + a.x+"_"+a.y+"_"+(17-b)+".jpg"
	}
			
			
	function inicio() {
		//local
		var ruta = "http://127.0.0.1/cake/datagro/";
		//pgtdemo
		//var ruta = "http://www.pgtdemo.com.ar/datagro/";
		
		if (GBrowserIsCompatible()) {

			// display the loading message
			//var om = new OverlayMessage(document.getElementById("map"));
			//om.Set("<b>Loading...</b>");
      		
			var map = new GMap2(document.getElementById("map"));
			map.removeMapType(G_NORMAL_MAP);
			map.removeMapType(G_HYBRID_MAP);
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());

			// ====== Creo mi propio tipo personalizado de mapa =====
      		var copyright = new GCopyright(1,
			new GLatLngBounds(new GLatLng(-90,-180),new GLatLng(90,180) ),
			14, "By <a href=\"http://www.pragmatia.com\">Pragmatia.com</a>");

			var copyrightCollection = new GCopyrightCollection("www.exactagro.com.ar - ");
			copyrightCollection.addCopyright(copyright);
		  
			var tilelayers = [new GTileLayer(copyrightCollection,10,19)];
			tilelayers[0].getTileUrl = CustomGetTileUrl;

			var custommap = new GMapType(tilelayers, new GMercatorProjection(20), "Propio", {alt:"Mapa Propio"});
			map.addMapType(custommap);

			// ====== Inicio el mapa con un tipo y un punto central =====
			map.setCenter(new GLatLng(-31.88179167,-63.73319833), 14);
			map.enableDoubleClickZoom();
			map.setMapType(G_SATELLITE_MAP);

			// ====== Restrinjo los niveles de zoom =====
			var mt = map.getMapTypes();
			// sobreescribo los metodos getMinimumResolution() y getMaximumResolution()
			
			for (var i=0; i < mt.length; i++) {
				mt[i].getMaximumResolution = function() {
					return 19;
				}
			}
			
			//agreo un evento al ZOOM level
			GEvent.addListener(map, "zoomend", function(oldzoom, newzoom) {
				
				if(newzoom >= 15) {
					map.removeMapType(G_SATELLITE_MAP);
					map.setMapType(custommap);
					ovmap.setMapType(G_SATELLITE_MAP);
				}
				else {
					map.addMapType(G_SATELLITE_MAP);
					map.setMapType(G_SATELLITE_MAP);
				}

				//a mas de zoom 18 saco el mapa overview
				var elDiv=document.getElementById("map_overview");
				if(newzoom >= 18) {
					elDiv.style.visibility = "hidden";
					elDiv.style.display = "none";
				}
				else {
					elDiv.style.visibility = "visible";
					elDiv.style.display = "inline";
				}
			});

			/*
       		var mm = new GMarkerManager(map);
			var marker=new GMarker(point);
			mm.addMarker(marker,0,17);
    
        			
			*/
			//Leo el archivo donde estan los datos
			var request = GXmlHttp.create();
			request.open("GET", ruta + "files/datos.xml", true);
			request.onreadystatechange = function() {
				if (request.readyState == 4) {
				
					var gmarkersA = [];
					var gmarkersB = [];
					var gmarkersC = [];
					var gmarkersD = [];

					var icon = new GIcon();
					icon.image = ruta + "img/marcadores/verde.png";
					icon.iconSize = new GSize(10, 10);
					icon.iconAnchor = new GPoint(6, 20);
					icon.infoWindowAnchor = new GPoint(5, 1);

					icono1 = new GIcon(icon,ruta + "img/marcadores/verde.png");
					icono2 = new GIcon(icon,ruta + "img/marcadores/amarillo.png");
					icono3 = new GIcon(icon,ruta + "img/marcadores/rojo.png");
					icono4 = new GIcon(icon,ruta + "img/marcadores/naranja.png");

					var xmlDoc = GXml.parse(request.responseText);
					//Obtengo un array de los marcadores y los recorro
					var markers = xmlDoc.documentElement.getElementsByTagName("marker");

					for (var i = 0; i < markers.length; i++) {
						// Obtengo los atributos de cada marcador
						var lat = parseFloat(markers[i].getAttribute("lat"));
						var lng = parseFloat(markers[i].getAttribute("lng"));
						var hum = parseFloat(markers[i].getAttribute("hum"));

						var punto = new GLatLng(lat, lng);
						// create the marker
						if(hum < 4.71) {
							var marker = crearMarcador(punto, icono1, \'<div style="width:240px">Humedad: \'+hum+\'</div>\')
							gmarkersA.push(marker);
						}
						else if(hum == 4.9) {
							var marker = crearMarcador(punto, icono2, \'<div style="width:240px">Humedad: \'+hum+\'</div>\')
							gmarkersB.push(marker);
						}
						else if(hum == 17.5) {
							var marker = crearMarcador(punto, icono3, \'<div style="width:240px">Humedad: \'+hum+\'</div>\')
							gmarkersB.push(marker);
						}
						else {
							var marker = crearMarcador(punto, icono4, \'<div style="width:240px">Humedad: \'+hum+\'</div>\')
							gmarkersC.push(marker);
						}
						//map.addOverlay(marker);
					} //for

					var mm = new GMarkerManager(map);
					mm.addMarkers(gmarkersA,0,17);
					//mm.addMarkers(gmarkersB,17,17);
					mm.addMarkers(gmarkersB,0,17);
					mm.addMarkers(gmarkersC,0,17);
					mm.addMarkers(gmarkersD,0,17);
					mm.refresh();
				}//if request.readyState
			}//request.onreadystatechange = function()
			request.send(null);




      //  ======== Add a map overview ==========
      //  ======== save a reference to the control =========
      var ovcontrol = new GOverviewMapControl(new GSize(200,200)); 
      map.addControl(ovcontrol);

      //  ======== A function to adjust the positioning of the overview ========
      function positionOverview(x,y) {
        var omap=document.getElementById("map_overview");
        omap.style.left = x+"px";
        omap.style.top = y+"px";
        
        // == restyling ==
        omap.firstChild.style.border = "1px solid gray";

        omap.firstChild.firstChild.style.left="4px";
        omap.firstChild.firstChild.style.top="4px";
        omap.firstChild.firstChild.style.width="190px";
        omap.firstChild.firstChild.style.height="190px";
      }
      
      // ======== get a reference to the GMap2 ===========
      var ovmap = ovcontrol.getOverviewMap();
      ovmap.setMapType(G_SATELLITE_MAP);
      
      GEvent.addListener(ovmap,"click",function() {alert("you clicked the overview");} );

      //  ======== change the overview map type AFTER the overview finisges initializing =====
      //setTimeout("ovmap.setMapType(G_SATELLITE_MAP);",1);
      			

      			
		}//GBrowserIsCompatible
	}//funcion inicio

	inicio();
');

echo $codigo_html;
?>